import CryptoJS from "crypto-js";
import moment from 'moment';

export function slsParams() {
    return {
        accessKeyId: 'LTAI4FnrYCyPyg64n8iU9jWU',
        secretAccessKey: 'gWve3SNkqLYZtcehP6Bi9isQNzgnhi',
        apiVersion: '2015-06-01',
        projectName: 'mvt',
        url: 'http://mvt.cn-hangzhou.sls.aliyuncs.com',
        host:'mvt.cn-hangzhou.sls.aliyuncs.com',
        apiversion: '0.6.0',
        signaturemethod: 'hmac-sha1',
        endpoints: {
            beijing:'http://cn-beijing.sls.aliyuncs.com',
            hangzhou:'http://cn-hangzhou.sls.aliyuncs.com',
            qingdao:'http://cn-qingdao.sls.aliyuncs.com',
            shenzhen:'http://cn-shenzhen.sls.aliyuncs.com'
        }
    }
}

export function getGMTTime(){
    // debugger
    // return moment(new Date()).utc().format("ddd, DD MMM YYYY HH:mm:ss GMT")
    return new Date().toUTCString()
}

/**
 * @param {*} data 
 * SignString = VERB + "\n"
             + CONTENT-MD5 + "\n"
             + CONTENT-TYPE + "\n"
             + DATE + "\n"
             + CanonicalizedLOGHeaders + "\n"
             + CanonicalizedResource
    //base64(hmac-sha1(UTF8-Encoding-Of(SignString)，AccessKeySecret))
 */
export function getSign(data){
    let verb = data.verb;
    let contenMd5 = data.contenMd5 ? data.contenMd5 : ""
    let contenType = data.contenType ? data.contenType : ""
    // let clh = "x-log-apiversion:0.6.0\nx-log-date:"+data.date+"\nx-log-signaturemethod:hmac-sha1"
    let clh = "x-log-apiversion:0.6.0\nx-log-signaturemethod:hmac-sha1"
    clh = data.canonicalizedLOGHeaders ? clh + data.canonicalizedLOGHeaders : clh
    let cr = data.canonicalizedResource ? data.canonicalizedResource : ""

    let singString = verb + "\n"
    + contenMd5 + "\n"
    + contenType + "\n"
    + data.date + "\n"
    + clh + "\n"
    + cr;

    singString = CryptoJS.HmacSHA1(CryptoJS.enc.Utf8.parse(singString), slsParams().secretAccessKey)
    return CryptoJS.enc.Base64.stringify(singString);
}